﻿package com.projects.xenia;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

import java.util.List;
import java.util.UUID;


import com.projects.xenia.global_modules.MainGlobalModule;

import com.projects.xenia.ref_classes.GoodsRefItem;


import android.content.ContentValues;
import android.database.Cursor;

public class Documents_Return {
	
	public DocHeader Head;
	public DocLine Line;
		
	public List<DocLine> DocLineItemsCollection = new ArrayList<DocLine> ();
	
	public class DocHeader {
		
		public String id;
		public int docnum;
		public Date date_create;
		public Date date_save;
		public Date date_execute;
		public int state;
		public int verstamp;
		public String acctype;
		public String firms_id;
		public String departments_id;
		public String user_id;
		public String employees_id;
		public String customers_id;
		public int planned;
		public double total_returnsum;		
		public int return_type;
			
	}
	
	public class DocLine {
		
		public String id;
		public String hdoc_id;
		public int lineno;
		public String goods_id;
		public String goods_feature_id;
		public String goods_measures_id;
		public double factor;
		public double return_value;
		public Date create_date;		
		public String return_base;		
		public double price;
		public double return_sum;
		public int date_type;
		
		public String goods_descr;
		public String return_base_descr;
		public String goods_measures_descr;
		
	}
	
	private int GetLastDocNumByCurrentDate () {
		
		int DocNum = 0;
		Cursor ScalarResult = MainGlobalModule.DBE.rawQuery("select max(docnum) from xdoc_returns_h", null);
		
		if (ScalarResult.getCount()>0) {
			ScalarResult.moveToFirst();
			DocNum = ScalarResult.getInt(0);								
		}
		
		ScalarResult.close();
		
		return (DocNum+1);
		
	}	
	
	public void OpenDoc (String Id) {

		Head = new DocHeader ();
		Line = new DocLine ();
		
		Cursor DocumentsResult = MainGlobalModule.DBE.rawQuery("select * from xdoc_returns_h where id = ?",new String [] {Id});
		
		DocumentsResult.moveToFirst();
			
		Head.id = DocumentsResult.getString(DocumentsResult.getColumnIndex("id"));
		Head.docnum = DocumentsResult.getInt(DocumentsResult.getColumnIndex("docnum"));
		Head.date_create = MainGlobalModule.SqlStringToDate (DocumentsResult.getString(DocumentsResult.getColumnIndex("date_create")));
		Head.date_save = MainGlobalModule.SqlStringToDate (DocumentsResult.getString(DocumentsResult.getColumnIndex("date_save"))); 
		Head.date_execute = MainGlobalModule.SqlStringToDate (DocumentsResult.getString(DocumentsResult.getColumnIndex("date_execute")));
		Head.state = DocumentsResult.getInt(DocumentsResult.getColumnIndex("state"));		
		Head.verstamp = DocumentsResult.getInt(DocumentsResult.getColumnIndex("verstamp"));
		Head.acctype = DocumentsResult.getString(DocumentsResult.getColumnIndex("acctype")); 
		Head.firms_id = DocumentsResult.getString(DocumentsResult.getColumnIndex("firms_id"));
		Head.departments_id = DocumentsResult.getString(DocumentsResult.getColumnIndex("departments_id"));		
		Head.user_id = DocumentsResult.getString(DocumentsResult.getColumnIndex("user_id"));
		Head.employees_id = DocumentsResult.getString(DocumentsResult.getColumnIndex("employees_id"));		
		Head.customers_id = DocumentsResult.getString(DocumentsResult.getColumnIndex("customers_id"));		
		Head.planned = DocumentsResult.getInt(DocumentsResult.getColumnIndex("planned"));
		Head.total_returnsum = DocumentsResult.getDouble(DocumentsResult.getColumnIndex("total_returnsum"));
		Head.return_type = DocumentsResult.getInt(DocumentsResult.getColumnIndex("return_type"));
		
		DocumentsResult.close();
		
		DocLineItemsCollection.clear();
		try {
			Cursor DocumentsLineResult = MainGlobalModule.DBE.rawQuery("select a.*,g.descr_long,b.descr,m.descr from xdoc_returns_t a left outer join xref_goods g on a.goods_id = g.id left outer join xref_goodsmeasures gm on a.goods_measures_id = gm.id left outer join xref_measures m on gm.measures_id = m.id left outer join xenum_ReturnBases b on a.return_base = b.id where a.hdoc_id = ?",new String [] {Id});			
		
		if (DocumentsLineResult.getCount()>0) {
			DocumentsLineResult.moveToFirst();
			
			do {
				
				DocLine DocLineItem = new DocLine();
				DocLineItem.id = DocumentsLineResult.getString(0);
				DocLineItem.hdoc_id = DocumentsLineResult.getString(1);
				DocLineItem.lineno = DocumentsLineResult.getInt(2);
				DocLineItem.goods_id = DocumentsLineResult.getString(3);
				DocLineItem.goods_feature_id = DocumentsLineResult.getString(4);
				DocLineItem.goods_measures_id = DocumentsLineResult.getString(5);
				DocLineItem.factor = DocumentsLineResult.getDouble(6);
				DocLineItem.return_value = DocumentsLineResult.getDouble(7);
				DocLineItem.create_date = MainGlobalModule.SqlStringToDate(DocumentsLineResult.getString(8));
				DocLineItem.return_base = DocumentsLineResult.getString(9);
				DocLineItem.price = DocumentsLineResult.getDouble(10);
				DocLineItem.return_sum = DocumentsLineResult.getDouble(11);
				DocLineItem.date_type = DocumentsLineResult.getInt(12);
				
				DocLineItem.goods_descr = DocumentsLineResult.getString(13);
				DocLineItem.return_base_descr = DocumentsLineResult.getString(14);
				DocLineItem.goods_measures_descr = DocumentsLineResult.getString(15);
				
				DocLineItemsCollection.add(DocLineItem);
				
			}
			
			while (DocumentsLineResult.moveToNext());
			DocumentsLineResult.close();
		}	

		} catch (Exception e) {
			e.printStackTrace();
		}

	}	
	
	public void NewDoc () {
		
		Head = new DocHeader ();
		Line = new DocLine ();
			
		Head.id = UUID.randomUUID().toString().toUpperCase();
		Head.docnum = GetLastDocNumByCurrentDate ();			
		Head.date_create = Calendar.getInstance().getTime();
		Head.date_save = Calendar.getInstance().getTime();
		Head.date_execute = Calendar.getInstance().getTime();
		Head.state = 1;
		Head.verstamp = 0;
		Head.acctype = MainGlobalModule.ConstantsCollection.get("DefAccType");
		Head.firms_id = MainGlobalModule.ConstantsCollection.get("DefFirm");
		Head.departments_id = MainGlobalModule.ConstantsCollection.get("DefDepartment");		
		Head.user_id = MainGlobalModule.SettingsCollection.get("ManagerId");
		Head.employees_id = MainGlobalModule.SettingsCollection.get("ManagerId");
		Head.customers_id = null;		
		Head.planned = 0;
		Head.total_returnsum = 0;
		Head.return_type = 0;
			
		InsertHeaderIntoDB ();	
		
	}

	private void InsertHeaderIntoDB () {
		
		ContentValues InsertValues = new ContentValues ();
		InsertValues.put("id", Head.id);
		InsertValues.put("docnum", Head.docnum);		
		InsertValues.put("date_create", MainGlobalModule.DateToSqlString (Head.date_create));
		InsertValues.put("date_save", MainGlobalModule.DateToSqlString (Head.date_save));
		InsertValues.put("date_execute", MainGlobalModule.DateToSqlString (Head.date_execute));		
		InsertValues.put("state", Head.state);
		InsertValues.put("verstamp", Head.verstamp);
		InsertValues.put("acctype", Head.acctype);
		InsertValues.put("firms_id", Head.firms_id);
		InsertValues.put("departments_id", Head.departments_id);
		InsertValues.put("user_id", Head.user_id);
		InsertValues.put("employees_id", Head.employees_id);
		InsertValues.put("customers_id", Head.customers_id);
		InsertValues.put("planned", Head.planned);
		InsertValues.put("total_returnsum", Head.total_returnsum);			
		InsertValues.put("return_type", Head.return_type);
		MainGlobalModule.DBE.insert("xdoc_returns_h", null, InsertValues);		
		
	}
		
	public void DeleteDoc () {
		
			Head.state = 3;
			UpdateDoc ();
		
	}	
	
	public void FinishDoc () {
		
		if (Head.customers_id==null){
		
			MainGlobalModule.DBE.beginTransaction();
			
				MainGlobalModule.DBE.delete("xdoc_returns_h", "id=?", new String []{Head.id});
				MainGlobalModule.DBE.delete("xdoc_returns_t", "hdoc_id=?", new String []{Head.id});
				MainGlobalModule.DBE.delete("xdoc_returns_d", "hdoc_id=?", new String []{Head.id});
				MainGlobalModule.DBE.delete("xdoc_gpslog", "id=?", new String []{Head.id});
			
			MainGlobalModule.DBE.setTransactionSuccessful();
			MainGlobalModule.DBE.endTransaction();
			
			
		}
		
	}
	
	public void UpdateDoc () {
		
			ContentValues UpdateValues = new ContentValues ();
			UpdateValues.put("id", Head.id);
			UpdateValues.put("docnum", Head.docnum);		
			UpdateValues.put("date_create", MainGlobalModule.DateToSqlString (Head.date_create));
			UpdateValues.put("date_save", MainGlobalModule.DateToSqlString (Calendar.getInstance().getTime()));
			UpdateValues.put("date_execute", MainGlobalModule.DateToSqlString (Head.date_execute));		
			UpdateValues.put("state", Head.state);
			UpdateValues.put("verstamp", Head.verstamp);
			UpdateValues.put("acctype", Head.acctype);
			UpdateValues.put("firms_id", Head.firms_id);
			UpdateValues.put("departments_id", Head.departments_id);
			UpdateValues.put("user_id", Head.user_id);
			UpdateValues.put("employees_id", Head.employees_id);
			UpdateValues.put("customers_id", Head.customers_id);
			UpdateValues.put("planned", Head.planned);
			UpdateValues.put("total_Returnsum", Head.total_returnsum);			
			UpdateValues.put("return_type", Head.return_type);
			
			MainGlobalModule.DBE.update("xdoc_Returns_h", UpdateValues, "id=?",new String[] {Head.id} );			
		
	}
	
	public DocLine GetDocLineByKey (String goods_id, String goods_measures_id, String return_base, Date create_date) {
		
		for (DocLine dl: DocLineItemsCollection){			
			if (dl.goods_id.equals(goods_id)&(dl.return_base.equals(return_base))&(dl.create_date.equals(create_date)&(dl.goods_measures_id.equals(goods_measures_id)))) {
				return dl;
			}			
		}		
		return null;		
		
	}
	
	public void InsertUpdateLine () {
		
		DocLine DocLineItem;
		
		if (DocLineItemsCollection.indexOf(Line)!=-1) {
			DocLineItem = DocLineItemsCollection.get(DocLineItemsCollection.indexOf(Line));	
		}	
		else
		{
			DocLineItem = GetDocLineByKey (Line.goods_id,Line.goods_measures_id,Line.return_base,Line.create_date);
			if (DocLineItem!=null) {
				DocLineItem.goods_measures_id = Line.goods_measures_id;
				DocLineItem.goods_measures_descr = Line.goods_measures_descr;			
				DocLineItem.factor = Line.factor;
				DocLineItem.return_value += Line.return_value; 
				DocLineItem.price = Line.price;
				DocLineItem.return_sum = Line.return_sum;
				DocLineItem.date_type = Line.date_type;  
				Line = DocLineItem;
			}			
		}		
		
		if ((Line.return_value==0) && (DocLineItem != null)) {			
			MainGlobalModule.DBE.delete("xdoc_returns_t", "id=?", new String [] {Line.id});
			DocLineItemsCollection.remove(DocLineItem);			
		}
		else
		{
			ContentValues InsertUpdateValues = new ContentValues ();
			InsertUpdateValues.put("id", Line.id);
			InsertUpdateValues.put("hdoc_id", Line.hdoc_id);
			InsertUpdateValues.put("lineno", Line.lineno);
			InsertUpdateValues.put("goods_id", Line.goods_id);
			InsertUpdateValues.put("goods_feature_id", Line.goods_feature_id);
			InsertUpdateValues.put("goods_measures_id", Line.goods_measures_id);
			InsertUpdateValues.put("factor", Line.factor);
			InsertUpdateValues.put("return_value", Line.return_value);			
			InsertUpdateValues.put("create_date", MainGlobalModule.DateToSqlString(Line.create_date));
			InsertUpdateValues.put("return_base", Line.return_base);			
			InsertUpdateValues.put("price", Line.price);
			InsertUpdateValues.put("return_sum", Line.return_sum);
			InsertUpdateValues.put("date_type", Line.date_type);
			
			if (DocLineItem!=null){
				MainGlobalModule.DBE.update("xdoc_returns_t", InsertUpdateValues, "id=?",new String[] {Line.id} );
			}
			else {
				DocLineItem = new DocLine ();
				MainGlobalModule.DBE.insert("xdoc_returns_t", null, InsertUpdateValues);
			}			
			
			if (!DocLineItemsCollection.contains(Line)){
				DocLineItemsCollection.add(Line);
			}			
			
		}	
		
		CalculateHeadSums ();
	
		
	}	
	
	public void NewLine () {
	
		Line = new DocLine ();
		
	}
	
	private void CalculateHeadSums () {

		Head.state = 1;
		
		Cursor qSum = MainGlobalModule.DBE.query("xdoc_Returns_t", new String [] {"sum(return_sum)"}, "hdoc_id=?", new String [] {Head.id}, null, null, null);
		qSum.moveToFirst();   
		
		Head.total_returnsum = qSum.getDouble(0);		
		
		UpdateDoc ();
		
		qSum.close();
		
	}	
	
	
	
	public void SetLineDocByPosition (int position) {
		
		Line = DocLineItemsCollection.get(position);		
			
	}
	
	
	public void SetLineDocByKey (GoodsRefItem gi, String return_base_id, Date create_date) {
		
		Line = GetDocLineByKey(gi.id, gi.measure_base_id, return_base_id, create_date);
		
		if (Line==null) {
			
			NewLine ();
			
			Line.id = UUID.randomUUID().toString().toUpperCase();
			Line.hdoc_id = Head.id;
			Line.lineno = DocLineItemsCollection.size();			
			Line.goods_id = gi.id;
			Line.goods_feature_id = "";
			Line.goods_measures_id = gi.measure_base_id;
			Line.factor = gi.measure_base_factor;
			Line.return_value = 0; 
			Line.create_date = null;
			Line.return_base = null;
			Line.price = gi.price;
			Line.return_sum = 0;
			Line.date_type = 0;
			
			Line.goods_descr = gi.descr;			
			Line.goods_measures_descr = gi.measure_base_descr;			
			Line.return_base_descr = "";	

		}
		
	}

}
